#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/7/13 19:33
# @Author  : jackkli
# @Site    : 
# @File    : qci_unittest.py
# @Software: PyCharm

import os
import re

if __name__ == '__main__':
    QCI_WORKSPACE = os.getenv('QCI_WORKSPACE', None)
    # QCI_WORKSPACE = "/Users/libobo/Desktop/GitCode/wemeet_proj/"
    unit_log = QCI_WORKSPACE + "unit_test/TestCase/bin/uttest.log"
    label_index = None
    with open(unit_log, "r", encoding='utf-8') as fr:
        uttest_list = fr.readlines()
        for uttest_line in uttest_list:
            if "Global test environment tear-down" in str(uttest_line):
                label_index = uttest_list.index(uttest_line)
        # print(uttest_list[label_index + 1])
        # print(uttest_list[label_index + 2])
        # print(uttest_list[label_index + 3])
    if label_index is None:
        print("wemeet_center unittest failed, please check code.")
        cmd = "cat {}".format(unit_log)
        print(os.system(cmd))
        raise Exception("wemeet_center unittest failed, please check code.")

    sum_num = int(re.findall(r"\d+\.?\d*", uttest_list[label_index + 1])[0])
    pass_num = int(re.findall(r"\d+\.?\d*", uttest_list[label_index + 2])[0])
    passing_rate = pass_num / sum_num * 100
    if passing_rate < 100:
        print("wemeet_center unittest failed，passing_rate:{}%".format(passing_rate))
        raise Exception("wemeet_center unittest failed，passing_rate:{}%".format(passing_rate))
    else:
        print("wemeet_center unittest success, passing_rate:{}%".format(passing_rate))
